package net.esj.basic.core.engine.util.tree;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

import net.esj.basic.core.engine.provider.AbstractProvidersFactory;
import net.esj.basic.pojo.TreeStructure;
import net.esj.basic.utils.CollectionUtils;

public final class TreeProvidersFactory extends AbstractProvidersFactory<TreeProvider> {

	public List<TreeStructure> findTree(String type) {
		List<TreeStructure> ret = Collections.EMPTY_LIST;
		for(TreeProvider manager : listProviders()){
			ret = manager.findTree(type);
			if(!CollectionUtils.isEmpty(ret)){
				break;
			}
		}
		return ret;
	}

	public TreeStructure getTree(String type, Serializable id) {
		TreeStructure ts = null;
		for(TreeProvider manager : listProviders()){
			ts = manager.getTree(type, id);
			if(ts!=null){
				break;
			}
		}
		return ts;
	}

	public List<TreeStructure> loadChildren(TreeStructure parent) {
		List<TreeStructure> ret = Collections.EMPTY_LIST;
		for(TreeProvider manager : listProviders()){
			ret = manager.loadChildren(parent);
			if(!CollectionUtils.isEmpty(ret)){
				break;
			}
		}
		return ret;
	}

	@Override
	protected Class<TreeProvider> getProviderClass() {
		return TreeProvider.class;
	}

	static TreeProvider proxy;
	
	@Override
	protected void provideProxy(TreeProvider proxy) {
		this.proxy = proxy;
	}

}
